import os
import pyJianYingDraft as draft
from pyJianYingDraft import Intro_type, Transition_type, trange
from pyJianYingDraft.keyframe import Keyframe_property
from pyJianYingDraft.time_util import SEC

tutorial_asset_dir = os.path.join(os.path.dirname(__file__), 'assets')

# 创建剪映草稿
script = draft.Script_file(1080, 1080) # 1080x1080分辨率

# 添加音频、视频和文本轨道
script.add_track(draft.Track_type.audio).add_track(draft.Track_type.video).add_track(draft.Track_type.text)

audio_material = draft.Audio_material(os.path.join(tutorial_asset_dir, 'test.mp3'))

script.add_material(audio_material)

audio_segment = draft.Audio_segment(audio_material,
                                    trange("0s", "529s"), # 片段将位于轨道上的0s-5s（注意5s表示持续时长而非结束时间）
                                    volume=0.6)  

script.add_segment(audio_segment)



# # 创建视频片段
# video_material = draft.Video_material(os.path.join(tutorial_asset_dir, 'test.png'))
# video_segment = draft.Video_segment(video_material,
#                                     draft.Timerange(0, 10)) # 与素材等长

# # 添加两个不透明度关键帧形成1s的淡出效果
# video_segment.add_keyframe(Keyframe_property.alpha, video_segment.duration - SEC, 1.0) # 结束前1s完全不透明
# video_segment.add_keyframe(Keyframe_property.alpha, video_segment.duration, 0.0) # 片段结束时完全透明
# # 添加素材及片段
# script.add_material(video_material)
# script.add_segment(video_segment)

sticker_material = draft.Video_material(os.path.join(tutorial_asset_dir, 'test.png'))
script.add_material(sticker_material)

sticker_segment = draft.Video_segment(sticker_material,
                                      trange(0, '10s'))

sticker_segment.add_keyframe(Keyframe_property.position_x, 0, -0.5)
sticker_segment.add_keyframe(Keyframe_property.position_x, '10s', 1)


script.add_segment(sticker_segment)

script.import_srt("./assets/test.srt", track_name="subtitle")


# 保存草稿（覆盖掉原有的draft_content.json）
script.dump("D:/Program Files (x86)/JianyingPro Drafts/sdfdsf/draft_content.json")